package com.qqtech.qquan.push.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.push.model.PushAudience;
import com.qqtech.qquan.push.model.PushAudienceQuery;
import com.qqtech.qquan.push.service.PushAudienceService;
import com.qqtech.qquan.user.model.UserValue;
import com.qqtech.qquan.user.service.UserValueService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/push/audience")
public class PushAudienceController extends BaseWebController<PushAudience, PushAudienceQuery> {
	@Resource
	private PushAudienceService pushAudienceService;
	@Resource
	private UserValueService userValueService;

	@Override
	protected BaseService<PushAudience> getBaseService() {
		return pushAudienceService;
	}

	/**
	 * 新增用户设备的推送注册id
	 * 
	 * @param pushAudienceQuery
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody PushAudienceQuery pushAudienceQuery) {
		Result result = new Result();
		Integer loginUserId = super.getUserId();
		String registrationId = pushAudienceQuery.getRegistrationId();
		if (pushAudienceQuery != null && StringUtil.isNotBlank(registrationId) && !super.isVisitor()) {
			pushAudienceService.addNewAudience(pushAudienceQuery.getRegistrationId(), loginUserId);
		}
		if (loginUserId != null && loginUserId.intValue() != 0 && StringUtil.isNotBlank(registrationId)) {
			UserValue userValue = new UserValue();
			userValue.setId(loginUserId);
			userValue.setRegistrationId(registrationId);
			userValue.setIsBackground(YesNoEnum.NO.getCode());
			userValueService.updateById(userValue);
		}
		return result;
	}
}
